//
//  SecurityViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/11.
//

import UIKit

class SecurityViewController: CommonViewController {
    private var dataList:Array<SessionItem> = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "账号与安全"
        let tableView = UITableView.init(frame: .zero, style: .grouped)
        view.addSubview(tableView)
        tableView.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })

        tableView.backgroundColor = BACKGROUND_COLOR
        tableView.separatorColor = Separator_COLOR
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = 44.0

        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SetItemArrowCell.self, forCellReuseIdentifier: "SettingArrowCell")
        tableView.register(SetItemSwitchCell.self, forCellReuseIdentifier: "SettingSwitchCell")
        
        dataList.append(SessionItem([SetItemModel.init(type: .Arrow, title: "手机号码",tip: "15311438582"){
                                        [weak self] in
                                            let alert = UIAlertController.init(title: "更换已绑定的手机号?", message: "当前绑定的手机号码为153****8582", preferredStyle: .alert)
                                            let cancel = UIAlertAction.init(title: "取消", style: .cancel)
                                            let out = UIAlertAction.init(title: "更换", style: .default) { (al) in
                                                
                                            }
                                            alert.addAction(cancel)
                                            alert.addAction(out)
                                            self?.present(alert, animated: true, completion: {
                                                
                                            })
                                    },
                                     
                                     SetItemModel.init(type: .Arrow, title: "修改密码"){
                                        [weak self] in
                                            let alert = UIAlertController.init(title: "修改登录密码", message: "将给手机153****8582发送验证码", preferredStyle: .alert)
                                            let cancel = UIAlertAction.init(title: "取消", style: .cancel)
                                            let out = UIAlertAction.init(title: "确认确定", style: .default) { (al) in
                                                
                                            }
                                            alert.addAction(cancel)
                                            alert.addAction(out)
                                            self?.present(alert, animated: true, completion: {
                                                
                                            })
                                     }]))
        dataList.append(SessionItem([SetItemModel.init(type: .Switch, title: "抖音"),
                                     SetItemModel.init(type: .Switch, title: "天翼")],title: "社交平台账号绑定"))

        dataList.append(SessionItem([SetItemModel.init(type: .Arrow, title: "账号注销"),
                                     SetItemModel.init(type: .Arrow, title: "登录设备管理"),
                                     SetItemModel.init(type: .Arrow, title: "头条安全中心",tip: "账号锁定或解除")],title: "高级设置"))
        tableView.reloadData()
    }

}
fileprivate struct SessionItem {
 var title:String = ""
 var list:[SetItemModel] = []
    init(_ list:[SetItemModel],title:String="") {
        self.list=list
        self.title = title
    }
}
extension SecurityViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let item = dataList[indexPath.section].list[indexPath.row]
        item.closure?()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        guard dataList[section].title == "" else {
            return 40.0
        }
        return 10.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
}
extension SecurityViewController:UITableViewDataSource{
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataList.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList[section].list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = dataList[indexPath.section].list[indexPath.row]
        if item.type == .Arrow {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingArrowCell", for: indexPath) as? SetItemArrowCell
            cell?.binding(item)
            return cell!
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingSwitchCell", for: indexPath) as? SetItemSwitchCell
            cell?.binding(item,indexPath:indexPath)
            return cell!
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let item = dataList[section]
        if(item.title != ""){
            let view = UIView()
            let titleL = UILabel()
            titleL.textColor = B999_TITLE_COLOR
            titleL.text = item.title
            titleL.font = Font_Size_12
            view.addSubview(titleL)
            titleL.snp.makeConstraints({
                $0.centerY.equalToSuperview().offset(7)
                $0.left.equalToSuperview().inset(15)
            })
            return view
        }
        return nil
    }
}
